clear
w=wg_load_data('X:\waveguide-data');


%% select 4um  width subset of measurements & integrate
subset=wg_filter(w,'width','eq',4);
subset=wg_filter(subset,'polarisation','eq','v');
subset=wg_filter(subset,'separation','eq',0);


% sort subset by waveguide length and then by scan number
[tmp ind]=sort([subset.length]'+[subset.scan]'*0.1);
subset=subset(ind);

x=zeros(size(subset));
y=zeros(size(subset));

% integrate
threshold=0.55;
for ii=1:length(subset)
    data=load(subset(ii).fullName);
    y(ii)=log(wg_integrate(data,threshold)/subset(ii).power);
    x(ii)=subset(ii).length;
    %pcolor(data);
    title([strrep(subset(ii).fullName,'\','\\') '    ' num2str(max(data(:)))])
end

%% plot length vs log(pout/pin) for 4um waveguides
figure(1)
clf

% plot data points
sc=scatter(x,y,36,'b','x');
xlim([0 24])
ylim 'auto'
ylim([0, max(ylim)])

% fit a polynomial of order 1 (i.e. a straight line) to the data
p=polyfit(x(x>0),y(x>0),1);
px=0:0.1:24;
py=polyval(p,px);
hold on
pl=plot(px,py,'r-');
hold off

% add labels
legend(pl,strrep(sprintf('y = -x/%.2fum + %.2f',[-1/p(1) p(2)]),'um','\mum'))
legend('boxoff');
ylabel('log(P_{out}/P_{in})')
xlabel('waveguide length /\mum')
fontsize=10;
set([gca get(gca,'Xlabel') get(gca,'Ylabel')],...
    'Fontsize',fontsize,'FontName','Times New Roman')


% position figure on paper & write pdf file
set(gca,'units','normalized')
set(gca,'position',[0.1 0.155 0.87 0.80])
set(gcf,'PaperSize',[12,6],'PaperPositionMode','auto',...
        'units','centimeters','paperposition',[0 0 12 6]);
figurepath='X:\josh-phd-code\cpgs\cpgs\4_ResultsAndDiscussion\ResultsAndDiscussionFigs\';
print('-dpdf',[figurepath 'wgPropLength']);


%% select 4um separation subset
oldpause=pause('query');
pause off
subset=wg_filter(w,'separation','eq',0);
subset=wg_filter(subset,'length','eq',16);
subset=wg_filter(subset,'width','eq',4);
subset=wg_filter(subset,'polarisation','eq','h');
%subset=wg_filter(subset,'scan','eq',3);


figure(gcf)
for ii=1:length(subset)
    data = load(subset(ii).fullName)';
    limits=load(strrep(subset(ii).fullName,'.txt','_limits.txt'));
    x=limits(1):limits(3):limits(2);
    y=limits(4):limits(6):limits(5);
    h=subplot(3,2,ii);
    pcolor(y(1:size(data,2)),x(1:size(data,1)),data);
    set(h,'color','none');
    axis equal
    colormap(hot);
    shading flat
    title(subset(ii).fullName,'interpreter','none');
    pause(2);
end
pause(oldpause);
%%
L=16;
W=4;

clear wg
subset=wg_filter(w,'separation','eq',0);
subset=wg_filter(subset,'length','eq',L);
subset=wg_filter(subset,'width','eq',W);
subset=wg_filter(subset,'polarisation','eq','v');
subset=wg_filter(subset,'scan','eq',3);
wg(1)=subset;

subset=wg_filter(w,'separation','ne',0);
subset=wg_filter(subset,'length','eq',L);
subset=wg_filter(subset,'width','eq',W);
subset=wg_filter(subset,'polarisation','eq','v');
subset=wg_filter(subset,'scan','eq',3);
wg(2)=subset;

subset=wg_filter(w,'separation','eq',0);
subset=wg_filter(subset,'length','eq',12);
subset=wg_filter(subset,'width','eq',4);
subset=wg_filter(subset,'polarisation','eq','h');
subset=wg_filter(subset,'scan','eq',1);
wg(3)=subset;




figure(1)
clf
fontsize=8;
for ii=1:length(wg)
    data = load(wg(ii).fullName)';
    limits=load(strrep(wg(ii).fullName,'.txt','_limits.txt'));
    x=limits(1):limits(3):limits(2);
    y=limits(4):limits(6):limits(5);
    x=x(1:size(data,1));
    y=y(1:size(data,2));
    jcb_contourplot(y,x,data);
    colormap(jcb_colormap(512,5));
    colorbar off
    set(gca,'xtick',[],'ytick',[],'box','off')
    pause(0.5)
    im = getframe(gca);
    [pathname imagename] = fileparts(wg(ii).fullName);
    imagepath = fullfile(mfilename('fullpath'),[imagename '_scan_image.png']);
    imwrite(im.cdata,imagepath,'png');
end


% position figure on paper & write pdf file
%set(gcf,'PaperSize',[16,4],'PaperPositionMode','auto',...
%        'units','centimeters','paperposition',[0 0 16 4]);
%figurepath='X:\josh-phd-code\cpgs\cpgs\4_ResultsAndDiscussion\ResultsAndDiscussionFigs\';
%print('-dpdf',[figurepath 'wgScans']);